﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections.Generic;

namespace W5_DoAnCuoiKy_Glow
{
    public class USER
    {
        //Khai báo biến
        private string _tenDN, _matKhau, _maTK;

        #region "Các hàm get & set"
        public string TenDN
        {
            get { return _tenDN; }
            set { _tenDN = value; }
        }

        public string MatKhau
        {
            get { return _matKhau; }
            set { _matKhau = value; }
        }

        public string MaTK
        {
            get { return _maTK; }
            set { _maTK = value; }
        }
        #endregion

        #region "Các hàm khởi tạo"
        public USER(string user, string pass, string maTK)
        {
            _tenDN = user;
            _matKhau = pass;
            _maTK = maTK;
        }

        public USER() 
        {
            _tenDN = "";
            _matKhau = "";
            _maTK = "";
        }
        #endregion

        #region "Thủ tục và hàm"
        //Lấy mã TK
        public string GetMaTK()
        {
            string sql = "select MaTaiKhoan from TAIKHOAN where TenDangNhap = '" + _tenDN.Replace("'", "''") + "'";
            DataTable dt = DataProvider.LoadSQL(sql);
            if (dt.Rows.Count > 0)
            {
                return dt.Rows[0][0].ToString();
            }
            else
            {
                return "";
            }
            

        }

        //Lấy loại TK
        public string GetLoaiTK()
        { 
            string sql = "Select LoaiTK from TAIKHOAN where MaTaiKhoan = '" + this.MaTK + "'";
            DataTable dt = DataProvider.LoadSQL(sql);
            return  dt.Rows[0][0].ToString();
        }
        //Kiểm tra user name và pass word
        public bool IsValidLogin()
        {
            string _matKhauMD5 = FunctionAndSub.MD5(this._matKhau);
            string sql = "select * from TAIKHOAN where TenDangNhap = '" + _tenDN.Replace("'", "''") + "' AND MatKhau = '" + _matKhauMD5.Replace("'", "''") + "'";
            using (DataTable dt = DataProvider.LoadSQL(sql))
            {
                bool ret = dt.Rows.Count > 0;
                return ret;
            }
        }

        //Kiểm tra tài khoản bị khóa
        public bool KiemTraTaiKhoanBiKhoa()
        {
            string sql = "select * from Block where MaTaiKhoan = '" + _maTK.Replace("'", "''") + "'";
            DataTable dt = DataProvider.LoadSQL(sql);
            DataRow row;
            DateTime _date = new DateTime();
            if (dt.Rows.Count > 0)
            {
                row = dt.Rows[0];
                _date = DateTime.Parse(row[2].ToString());
            }
            if (_date > DateTime.Now)
            {
                return true;
            }
            return false;
        }

        //Kiểm tra tài khoản có tồn tại hay không
        public bool KiemTraTaiKhoanTonTai()
        {
            string sql = "select * from TAIKHOAN where MaTaiKhoan = '" + _maTK.Replace("'", "''") + "'";
            using (DataTable dt = DataProvider.LoadSQL(sql))
            {
                bool ret = dt.Rows.Count > 0;
                return ret;
            }

        }
        
        public string GetTenTK()
        {

            string sql = "select HoTen from TAIKHOAN where MaTaiKhoan = '" + _maTK.Replace("'", "''") + "'";
            using (DataTable dt = DataProvider.LoadSQL(sql))
            {
                string tenTK = dt.Rows[0][0].ToString();
                return tenTK;
            }
        }

        #endregion
    }
}
